package cn.kginglam.dynamic_excel.common.utils;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

/**
 * @author: Lam
 * @date: 2021-09-03 10:58
 * @description:
 */
public class ReflectUtils {


    private static final Logger logger = LoggerFactory.getLogger(ReflectUtils.class);

    public static Object invokeGet(Object obj, String fieldName) {
        Class<?> aClass = obj.getClass();
        try {
            if (StrUtil.containsAny(fieldName, "serialVersionUID", "OPT_EXPORT_FLAG", "UN_OPT_EXPORT_FLAG")) {
                return null;
            }

            // 获取对应的get方法
            StringBuffer methodName = new StringBuffer();
            methodName.append("get")
                    .append(fieldName.substring(0, 1).toUpperCase())
                    .append(fieldName.substring(1));


            // 调用对应的get方法
            Method method = aClass.getMethod(methodName.toString());
            return method.invoke(obj);

        } catch (Exception e) {
            logger.error("找不到对应的方法====>{}", e.getMessage());
            return null;
        }
    }
}
